home *** CD-ROM | disk | FTP | other *** search
/ Die Ultimative Software-P…i Collection 1996 & 1997 / Die Ultimative Software-Pakete CD-ROM fur Atari Collection 1996 & 1997.iso / g / gnu_c / pmlsrc23.zoo / pmlsrc / cosh.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-19  |  5.1 KB  |  259 lines

  1. /************************************************************************
  2.  *                                    *
  3.  *                N O T I C E                *
  4.  *                                    *
  5.  *            Copyright Abandoned, 1987, Fred Fish        *
  6.  *                                    *
  7.  *    This previously copyrighted work has been placed into the    *
  8.  *    public domain by the author (Fred Fish) and may be freely used    *
  9.  *    for any purpose, private or commercial.  I would appreciate    *
  10.  *    it, as a courtesy, if this notice is left in all copies and    *
  11.  *    derivative works.  Thank you, and enjoy...            *
  12.  *                                    *
  13.  *    The author makes no warranty of any kind with respect to this    *
  14.  *    product and explicitly disclaims any implied warranties of    *
  15.  *    merchantability or fitness for any particular purpose.        *
  16.  *                                    *
  17.  ************************************************************************
  18.  */
  19.  
  20.  
  21. /*
  22.  *  FUNCTION
  23.  *
  24.  *    cosh   double precision hyperbolic cosine
  25.  *
  26.  *  KEY WORDS
  27.  *
  28.  *    cosh
  29.  *    machine independent routines
  30.  *    math libraries
  31.  *
  32.  *  DESCRIPTION
  33.  *
  34.  *    Returns double precision hyperbolic cosine of double precision
  35.  *    floating point number.
  36.  *
  37.  *  USAGE
  38.  *
  39.  *    double cosh (x)
  40.  *    double x;
  41.  *
  42.  *  REFERENCES
  43.  *
  44.  *    Fortran IV plus user's guide, Digital Equipment Corp. pp B-4
  45.  *
  46.  *  RESTRICTIONS
  47.  *
  48.  *    Inputs greater than log(MAXDOUBLE) result in overflow.
  49.  *    Inputs less than log(MINDOUBLE) result in underflow.
  50.  *
  51.  *    For precision information refer to documentation of the
  52.  *    floating point library routines called.
  53.  *    
  54.  *  PROGRAMMER
  55.  *
  56.  *    Fred Fish
  57.  *
  58.  *    68881 support added by Michael Ritzert
  59.  *
  60.  *  INTERNALS
  61.  *
  62.  *    Computes hyperbolic cosine from:
  63.  *
  64.  *        cosh(X) = 0.5 * (exp(X) + exp(-X))
  65.  *
  66.  */
  67.  
  68. #include <stdio.h>
  69. #include <math.h>
  70. #include "pml.h"
  71.  
  72. #if !defined (__M68881__) && !defined (sfp004)    /* mjr++        */
  73.  
  74. static char funcname[] = "cosh";
  75.  
  76.     struct exception xcpt;
  77.  
  78. double cosh (x)
  79. double x;
  80. {
  81.     if (x > LOGE_MAXDOUBLE) {
  82.     xcpt.type = OVERFLOW;
  83.     xcpt.name = funcname;
  84.     xcpt.arg1 = x;
  85.     if (!matherr (&xcpt)) {
  86.         fprintf (stderr, "%s: OVERFLOW error\n", funcname);
  87.         errno = ERANGE;
  88.         xcpt.retval = MAXDOUBLE;
  89.     }
  90.     } else if (x < LOGE_MINDOUBLE) {
  91.     xcpt.type = UNDERFLOW;
  92.     xcpt.name = funcname;
  93.     xcpt.arg1 = x;
  94.     if (!matherr (&xcpt)) {
  95.         fprintf (stderr, "%s: UNDERFLOW error\n", funcname);
  96.         errno = ERANGE;
  97.         xcpt.retval = MINDOUBLE;
  98.     }
  99.     } else {
  100.     x = exp (x);
  101.     xcpt.retval = 0.5 * (x + 1.0/x);
  102.     }
  103.     return (xcpt.retval);
  104. }
  105. #endif    /* !__M68881__ && !sfp004    */
  106. #ifdef    sfp004
  107. __asm("
  108.  
  109. comm =     -6
  110. resp =    -16
  111. zahl =      0
  112.  
  113. ");    /* end asm    */
  114.  
  115. #endif    sfp004
  116. #if defined (__M68881__) || defined (sfp004)
  117.     __asm(".text; .even");
  118.  
  119. # ifdef    ERROR_CHECK
  120.  
  121.     __asm("
  122.  
  123. _Overflow:
  124.     .ascii \"OVERFLOW\\0\"
  125. _Domain:
  126.     .ascii \"DOMAIN\\0\"
  127. _Error_String:
  128.     .ascii \"cosh: %s error\\n\\0\"
  129. .even
  130. | pml compatible coshgent
  131. | m.ritzert 7.12.1991
  132. | ritzert@dfg.dbp.de
  133. |
  134. |    /* NAN  = {7fffffff,ffffffff}        */
  135. |    /* +Inf = {7ff00000,00000000}        */
  136. |    /* -Inf = {fff00000,00000000}        */
  137. |    /* MAX_D= {7fee42d1,30773b76}        */
  138. |    /* MIN_D= {ffee42d1,30773b76}        */
  139.  
  140. .even
  141. double_max:
  142.     .long    0x7fee42d1
  143.     .long    0x30273b76
  144. double_min:
  145.     .long    0xffee42d1
  146.     .long    0x30273b76
  147. NaN:
  148.     .long    0x7fffffff
  149.     .long    0xffffffff
  150. p_Inf:
  151.     .long    0x7ff00000
  152.     .long    0x00000000
  153. m_Inf:
  154.     .long    0xfff00000
  155.     .long    0x00000000
  156.     ");    /* end asm    */
  157. # endif    ERROR_CHECK
  158.  
  159. __asm("
  160. .even
  161.     .globl _cosh
  162. _cosh:
  163.     ");    /* end asm    */
  164.  
  165. #endif    /* __M68881__ || sfp004    */
  166. #ifdef    __M68881__
  167.  
  168.     __asm("
  169.     fcoshd    a7@(4), fp0    | cosh
  170.     fmoved    fp0,a7@-    | push result
  171.     moveml    a7@+,d0-d1    | return_value
  172.     ");    /* end asm    */
  173.  
  174. #endif    __M68881__
  175. #ifdef    sfp004
  176.     __asm("
  177.     lea    0xfffa50,a0
  178.     movew    #0x5419,a0@(comm)    | specify function
  179.     cmpiw    #0x8900,a0@(resp)    | check
  180.     movel    a7@(4),a0@        | load arg_hi
  181.     movel    a7@(8),a0@        | load arg_low
  182.     movew    #0x7400,a0@(comm)    | result to d0
  183.     .long    0x0c688900, 0xfff067f8    | wait
  184.     movel    a0@,d0
  185.     movel    a0@,d1
  186.     ");    /* end asm    */
  187.  
  188. #endif    sfp004
  189. #if defined (__M68881__) || defined (sfp004)
  190. # ifdef    ERROR_CHECK
  191.     __asm("
  192.     lea    double_max,a0    |
  193.     swap    d0        | exponent into lower word
  194.     cmpw    a0@(16),d0    | == NaN ?
  195.     beq    error_nan    |
  196.     cmpw    a0@(24),d0    | == + Infinity ?
  197.     beq    error_plus    |
  198.     cmpw    a0@(32),d0    | == - Infinity ?
  199.     beq    error_minus    |
  200.     swap    d0        | result ok,
  201.     rts            | restore d0
  202. ");
  203. #ifndef    __MSHORT__
  204. __asm("
  205. error_minus:
  206.     swap    d0
  207.     moveml    d0-d1,a7@-
  208.     movel    #63,_errno    | errno = ERANGE
  209.     pea    _Overflow    | for printf
  210.     bra    error_exit    |
  211. error_plus:
  212.     swap    d0
  213.     moveml    d0-d1,a7@-
  214.     movel    #63,_errno    | NAN => errno = EDOM
  215.     pea    _Overflow    | for printf
  216.     bra    error_exit    |
  217. error_nan:
  218.     moveml    a0@(24),d0-d1    | result = +inf
  219.     moveml    d0-d1,a7@-
  220.     movel    #62,_errno    | NAN => errno = EDOM
  221.     pea    _Domain        | for printf
  222. ");
  223. #else    __MSHORT__
  224. __asm("
  225. error_minus:
  226.     swap    d0
  227.     moveml    d0-d1,a7@-
  228.     movew    #63,_errno    | errno = ERANGE
  229.     pea    _Overflow    | for printf
  230.     bra    error_exit    |
  231. error_plus:
  232.     swap    d0
  233.     moveml    d0-d1,a7@-
  234.     movew    #63,_errno    | NAN => errno = EDOM
  235.     pea    _Overflow    | for printf
  236.     bra    error_exit    |
  237. error_nan:
  238.     moveml    a0@(24),d0-d1    | result = +inf
  239.     moveml    d0-d1,a7@-
  240.     movew    #62,_errno    | NAN => errno = EDOM
  241.     pea    _Domain        | for printf
  242. ");
  243. #endif    __MSHORT__
  244. __asm("
  245. error_exit:
  246.     pea    _Error_String    |
  247.     pea    __iob+52    |
  248.     jbsr    _fprintf    |
  249.     addl    #12,a7        |
  250.     moveml    a7@+,d0-d1
  251.     rts
  252.     ");
  253. # else    ERROR_CHECK
  254.  
  255. __asm("rts");
  256.  
  257. # endif    ERROR_CHECK
  258. #endif /* __M68881__ || sfp004    */
  259.